package com.feib.ebilling.job.util;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringUtils {
    
	public static Logger logger = LoggerFactory.getLogger(StringUtils.class);
	
    public static String PadString(String sWay, String sType, int iCount, String sData) {
    	
    	if (sData == null) sData = "";    	
        int iLength = (iCount - sData.getBytes().length)>0 ? iCount - sData.getBytes().length : 0;	
        if (sWay.toUpperCase().equals("L")) {
            for (int i=0; i<iLength; i++) {
                sData = sData + sType;
            }
        } else if (sWay.toUpperCase().equals("R")) {
            for (int i=0; i<iLength; i++) {
                sData = sType + sData;
            }
        }
        return sData;
    }
    
    public static String PadStringChinese(String sWay, String sType, int iCount, String sData) {
    	
    	if (sData == null) sData = "";
    	int len = 0;
    	try{   
    	   len = new String(sData.getBytes("big5"),"8859_1").length();
    	}catch( Exception e){   
    	   logger.error("[錯誤]" + e.getMessage(), e);
        }   
        int iLength = (iCount - len)>0 ? iCount - len : 0;	
        if (sWay.toUpperCase().equals("L")) {
            for (int i=0; i<iLength; i++) {
                sData = sData + sType;
            }
        } else if (sWay.toUpperCase().equals("R")) {
            for (int i=0; i<iLength; i++) {
                sData = sType + sData;
            }
        }
        return sData;
    }
 
    public static String GetStrCDate() {

        String sSDate = "";
        Calendar calendar=new GregorianCalendar();
        Date nowTime=new Date();
        calendar.setTime(nowTime);
        String strYear=String.valueOf(calendar.get(Calendar.YEAR));        
        String strMonth=String.valueOf(calendar.get(Calendar.MONTH)+1);
        String strDay=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        if (Integer.parseInt(strYear)-1911 >= 100){
            sSDate = String.valueOf(Integer.parseInt(strYear)-1911) + PadString("R","0",2,strMonth) + PadString("R","0",2,strDay);
        }else{
            sSDate = "0" +String.valueOf(Integer.parseInt(strYear)-1911) + PadString("R","0",2,strMonth) + PadString("R","0",2,strDay);
        }
        return sSDate;
    }

    public static String GetStrCDateDiff(String strDate, int iDays) {

        int intYear = Integer.parseInt(strDate.substring(0, 3)) + 1911;
        int intMonth = Integer.parseInt(strDate.substring(3, 5));
        int intDate = Integer.parseInt(strDate.substring(5, 7));

        Calendar calendar = Calendar.getInstance();
        calendar.set(intYear, intMonth - 1, intDate);
        calendar.add(Calendar.DATE, iDays);

        String strYear=String.valueOf(calendar.get(Calendar.YEAR));
        String strMonth=String.valueOf(calendar.get(Calendar.MONTH)+1);
        String strDay=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        if (Integer.parseInt(strYear)-1911 >= 100){
            strDate = String.valueOf(Integer.parseInt(strYear)-1911) + PadString("R","0",2,strMonth) + PadString("R","0",2,strDay);
        }else{
            strDate = "0" +String.valueOf(Integer.parseInt(strYear)-1911) + PadString("R","0",2,strMonth) + PadString("R","0",2,strDay);
        }
        return strDate;
    }

    public static String ConvCDate2EDate(String strDate) {
    	
        int intYear = Integer.parseInt(strDate.substring(0, 3)) + 1911;
        int intMonth = Integer.parseInt(strDate.substring(3, 5));
        int intDate = Integer.parseInt(strDate.substring(5, 7));
        return  PadString("R","0",4,String.valueOf(intYear)) + 
                PadString("R","0",2,String.valueOf(intMonth)) + 
                PadString("R","0",2,String.valueOf(intDate));       
    }
    
    public static String ConvEDate2CDate(String strDate) {
    	
        int intYear = Integer.parseInt(strDate.substring(0, 4)) - 1911;
        int intMonth = Integer.parseInt(strDate.substring(4, 6));
        int intDate = Integer.parseInt(strDate.substring(6, 8));
        return  PadString("R","0",4,String.valueOf(intYear)) + 
                PadString("R","0",2,String.valueOf(intMonth)) + 
                PadString("R","0",2,String.valueOf(intDate));       
    }
    
    public static boolean isInteger(String input) {   
        try{   
            Integer.parseInt( input );   
            return true;   
        }catch(Exception e){   
            return false;   
        }   
    }  
    
    public static boolean isLong(String input) {   
        try{   
            Long.parseLong( input );   
            return true;   
        }catch(Exception e){   
            return false;   
        }   
    }   
   
	public static String DBCSsubstring(String str, int startPos, int endPos) {
		
		if  (DBCSlen(str) <  endPos || endPos == 0 || endPos < startPos )  return "?";
		int len = endPos - startPos;		
		byte[] tmp = new byte[len];
        try {
			System.arraycopy(str.getBytes("Big5"), startPos, tmp, 0, len);
		} catch (UnsupportedEncodingException e) {
			return "";
		}
        return new String(tmp);
	}
	
    public static int DBCSlen(String FromStr) {
   	
      if(FromStr == null) return 0;
      int FromLen = FromStr.length();
      int DBCSLen = 0;
	  for(int i = 0;i<FromLen;i++)  {
	     if(big5Value(FromStr.charAt(i)) > 0){
	    	DBCSLen = DBCSLen + 2;
	     }else{
	    	DBCSLen ++;		
	     }
	  }
      return DBCSLen;
   }
 
   public static int big5Value(char ch) {

      String str = new String();
      str += ch;
      try{
	     byte[] bytes = str.getBytes("Big5");
	     if (bytes.length < 2) return 0;
	     return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);
      }catch (Exception e) {
         return 0;
      }
   }

}
